<template>
    <div class="views-jiedan-updt">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 添加接单 </span>
                    </div>
                </template>

                <el-form :model="form" ref="formModel" :label-width="labelWidth" status-icon validate-on-rule-change>
                    <el-form-item v-if="isRead" label="项目编号" prop="xiangmubianhao"> {{ form.xiangmubianhao }} </el-form-item>

                    <el-form-item v-if="isRead" label="项目名称" prop="xiangmumingcheng"> {{ form.xiangmumingcheng }} </el-form-item>

                    <el-form-item v-if="isRead" label="项目分类" prop="xiangmufenlei">
                        <e-select-view module="fuwufenlei" :value="form.xiangmufenlei" select="id" show="fenleimingcheng"></e-select-view>
                    </el-form-item>

                    <el-form-item v-if="isRead" label="价格" prop="jiage" :rules="[{validator:rule.checkNumber, message:'输入一个有效数字'}]"> {{ form.jiage }} </el-form-item>

                    <el-form-item v-if="isRead" label="预约时间" prop="yuyueshijian"> {{ form.yuyueshijian }} </el-form-item>

                    <el-form-item v-if="isRead" label="预约时段" prop="yuyueshiduan"> {{ form.yuyueshiduan }} </el-form-item>

                    <el-form-item v-if="isRead" label="姓名" prop="xingming"> {{ form.xingming }} </el-form-item>

                    <el-form-item v-if="isRead" label="联系方式" prop="lianxifangshi"> {{ form.lianxifangshi }} </el-form-item>

                    <el-form-item v-if="isRead" label="地址" prop="dizhi"> {{ form.dizhi }} </el-form-item>

                    <el-form-item v-if="isRead" label="预约人" prop="yuyueren"> {{ form.yuyueren }} </el-form-item>

                    <el-form-item label="备注" prop="beizhu"> <el-input type="textarea" v-model="form.beizhu"></el-input> </el-form-item>

                    <el-form-item label="护工" prop="hugong">
                        <div style="width: 100%">
                            <e-select-list
                                :model="form"
                                v-model="form.hugong"
                                module="hugong"
                                select-update="gonghao,hugongxingming,hugongdianhua"
                                search-update=""
                            ></e-select-list>
                            <table>
                                <tr>
                                    <td width="120">工号</td>
                                    <td>{{ form.gonghao }}</td>
                                </tr>
                                <tr>
                                    <td width="120">护工姓名</td>
                                    <td>{{ form.hugongxingming }}</td>
                                </tr>
                                <tr>
                                    <td width="120">护工电话</td>
                                    <td>{{ form.hugongdianhua }}</td>
                                </tr>
                            </table>
                        </div>
                    </el-form-item>

                    <el-form-item label="工号" prop="gonghao"> <el-input v-model="form.gonghao" readonly style="width: 250px"></el-input> </el-form-item>

                    <el-form-item label="护工姓名" prop="hugongxingming">
                        <el-input type="text" placeholder="输入护工姓名" style="width: 450px" v-model="form.hugongxingming" />
                    </el-form-item>

                    <el-form-item label="护工电话" prop="hugongdianhua">
                        <el-input type="text" placeholder="输入护工电话" style="width: 450px" v-model="form.hugongdianhua" />
                    </el-form-item>

                    <el-form-item label="共享定位" prop="gongxiangdingwei">
                        <e-baidu-address v-model="form.gongxiangdingwei" v-model:lng="form.jingdu" v-model:lat="form.weidu" v-model:hash="form.hash"></e-baidu-address>
                    </el-form-item>

                    <el-form-item label="操作人" prop="caozuoren"> <el-input v-model="form.caozuoren" readonly style="width: 250px"></el-input> </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form></el-card
            >
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";

    import { ref, reactive, computed, watch } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useJiedanFindById, canJiedanFindById, canJiedanUpdate, canFuwuyuyueFindById } from "@/module";
    import { extend } from "@/utils/extend";

    const route = useRoute();
    const props = defineProps({
        id: [String, Number],
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
        labelWidth: {
            type: String,
            default: "140px",
        },
    });
    const form = useJiedanFindById(props.id);
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);

    const submit = () => {
        formModel.value.validate().then((res) => {
            if (loading.value) return;
            loading.value = true;
            canJiedanUpdate(form).then(
                (res) => {
                    loading.value = false;
                    if (res.code == 0) {
                        emit("success", res.data);
                        if (props.isHouxu) {
                            ElMessage.success("更新成功");
                            router.go(-1);
                        }
                    } else {
                        ElMessageBox.alert(res.msg);
                    }
                },
                (err) => {
                    loading.value = false;
                    ElMessageBox.alert(err.message);
                }
            );
        });
    };

    const readMap = reactive({});
    watch(
        () => form.fuwuyuyueid,
        (id) => {
            canFuwuyuyueFindById(id).then((res) => {
                extend(readMap, res);
            });
        }
    );
</script>

<style scoped lang="scss">
    .views-jiedan-updt {
    }
</style>
